package com.freez.spark.core.operation;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.PairFunction;
import org.apache.spark.api.java.function.VoidFunction;
import scala.Tuple2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * FREEDOM  2021 人生苦短，不妨一试
 * FileName: SortAndGroup.java
 * Author: zcs
 * Date: 2021年-12月-07日 周二 18:16
 * Description: 对sortByKey,groupByKey算子进行演示
 */
public class SortAndGroup {
    public static void main(String[] args) {
        SparkConf conf = new SparkConf();
        conf.setAppName("SparkCore-SortAndGroup").setMaster("local");
        JavaSparkContext jsc = new JavaSparkContext(conf);
        //数据
        List<Tuple2<String, Integer>> dataList = new ArrayList<>();
        dataList.add(new Tuple2<String, Integer>("2020-05-01", 11));
        dataList.add(new Tuple2<String, Integer>("2020-05-02", 4));
        dataList.add(new Tuple2<String, Integer>("2020-05-02", 7));
        dataList.add(new Tuple2<String, Integer>("2020-05-01", 4));
        dataList.add(new Tuple2<String, Integer>("2020-05-01", 10));
        JavaPairRDD<String, Integer> aRdd = jsc.parallelizePairs(dataList);
        JavaRDD<String> bRdd = jsc.parallelize(Arrays.asList("hello world", "hello hi"));
        /**
         * sortByKey
         */
        JavaRDD<Integer> sortRDD = jsc.parallelize(Arrays.asList(1, 3, 5, 2, 8, 4));
        JavaPairRDD<String, Integer> finalRDD = sortRDD.mapToPair(new PairFunction<Integer, String, Integer>() {
            @Override
            public Tuple2<String, Integer> call(Integer integer) throws Exception {
                return new Tuple2<>(integer.toString(), integer);
            }
        }).sortByKey(false);
        /**
         * groupByKey
         */
        finalRDD.foreach((VoidFunction<Tuple2<String, Integer>>) s -> System.out.println(s));
        aRdd.groupByKey().foreach(new VoidFunction<Tuple2<String, Iterable<Integer>>>() {
            @Override
            public void call(Tuple2<String, Iterable<Integer>> s) throws Exception {
                System.out.println(s);
            }
        });

    }
}